//
//  LoginViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/7.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "LoginViewController.h"
#import "UIButton+Block.h"
#import "MassageTabBarViewController.h"
#import "AppDelegate.h"
#import "LicensedTabBarViewController.h"
#import "UIView+BlockGesture.h"
@interface LoginViewController ()
{
    int type;
    
}
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeUI];
    NSLog(@"-------%@",self.view.subviews);
    
      
    
    // Do any additional setup after loading the view from its nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.phoneNumber resignFirstResponder];
    [self.passWord resignFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden=NO
    ;
    [AppDelegate shareInstance].rootNav.navigationBar.hidden=YES;
    
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
    //     [AppDelegate shareInstance].rootNav.navigationBar.hidden=YES;
    
}

-(void)makeUI
{
    /**
     *  默认为执业医师选项
     */
    type=4;
    _zhiYeDoctor.selected = YES;
    
    _tuiNaDoctors.selected=NO;
    
    if (_tuiNaDoctors.selected==NO) {
        _tuiNaDoctors.backgroundColor=[UIColor clearColor];
        [_tuiNaDoctors setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        
    }else{
        _tuiNaDoctors.backgroundColor=[UIColor whiteColor];
        
    }
    
    
    _tuiNaDoctorDaoHangImg.hidden=YES;
//    ADD_TARGET_INSIDE(_zhiYeDoctor, zhiyePress:);
//    ADD_TARGET_INSIDE(_tuiNaDoctors, _tuiNaDocors:);
    

    [_zhiYeDoctor addActionHandler:^(NSInteger tag) {
        
        
        
        _tuiNaDoctors.backgroundColor=[UIColor clearColor];
        _zhiYeDoctor.backgroundColor=[UIColor whiteColor];
        [_tuiNaDoctors setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        
        [_zhiYeDoctor setTitleColor:UIColorFromRGB(0x00ccc2) forState:UIControlStateNormal];
        _daoHangImg.hidden=NO;
        _tuiNaDoctorDaoHangImg.hidden=YES;
        type=4;
        
           NSLog(@"-------%@",self.view.subviews);
        
        
    }];
    
    
    
    
    
    [_tuiNaDoctors addActionHandler:^(NSInteger tag) {
        [_tuiNaDoctors setTitleColor:UIColorFromRGB(0x00ccc2) forState:UIControlStateNormal];
        [_zhiYeDoctor setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        _tuiNaDoctors.backgroundColor=[UIColor whiteColor];
        _zhiYeDoctor.backgroundColor=[UIColor clearColor];
        _daoHangImg.hidden=YES;
        _tuiNaDoctorDaoHangImg.hidden=NO;
        NSLog(@"-------%@",self.view.subviews);
        
        //        UIView
        
        //         [UIView animateWithDuration:2 animations:^{
        //             _daoHangImg.frame=CGRectMake(300, _daoHangImg.top, _daoHangImg.width, _daoHangImg.width);
        //
        //
        //         }];
        
        NSLog(@"%f",_daoHangImg.left);
        
        type=1;
        
    }];
    
    
    
    UIImage *image = IMAGE(@"bg_login");
    self.view.layer.contents = (id) image.CGImage;
    _passWord.layer.borderColor=UIColorFromRGB(0xffffff).CGColor;
    _passWord.layer.borderWidth=1;
    _phoneNumber.layer.borderColor=UIColorFromRGB(0xffffff).CGColor;
    _phoneNumber.layer.borderWidth=1;
    _zhiYeDoctor.layer.borderColor=UIColorFromRGB(0xffffff).CGColor;
    _zhiYeDoctor.layer.borderWidth=1;
    _tuiNaDoctors.layer.borderColor=UIColorFromRGB(0xffffff).CGColor;
    _tuiNaDoctors.layer.borderWidth=1;
    
    UIView* view1 = [[UIView alloc]initWithFrame:CGRectMake(0,0,35,50)];
    
    UIImageView *_VerificationimageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_user"]];
    _phoneNumber.leftViewMode = UITextFieldViewModeAlways;
    _phoneNumber.keyboardType=UIKeyboardTypeNumberPad;
    [view1 addSubview:_VerificationimageView
     ];
    
    _VerificationimageView.frame=CGRectMake(9, 16, 16, 18);
    
    _phoneNumber.leftView=view1;
    
    
    
    UIView* view2 = [[UIView alloc]initWithFrame:CGRectMake(0,0,35,50)];
    UIImageView *shoujiimageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_mima"]];
    shoujiimageView.frame=CGRectMake(9, 16, 16, 18);
    
    [view2 addSubview:shoujiimageView];
    
    
    _passWord.leftViewMode = UITextFieldViewModeAlways;
    _passWord.leftView=view2;
    _passWord.keyboardType=UIKeyboardTypeDefault;
    
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_phoneNumber.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft
                                                         cornerRadii:CGSizeMake(2, 2)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _phoneNumber.bounds;
    maskLayer.path = maskPath.CGPath;
    _phoneNumber.layer.mask = maskLayer;
    
    
    UIBezierPath *_posMask = [UIBezierPath bezierPathWithRoundedRect:_passWord.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                                         cornerRadii:CGSizeMake(2, 2)];
    CAShapeLayer *posLay = [[CAShapeLayer alloc] init];
    posLay.frame = _passWord.bounds;
    posLay.path = _posMask.CGPath;
    _passWord.layer.mask = posLay;
    
//    
//    ALLOC(UIButton, tempButton);
//    FRAME(tempButton, 20, 100, 100, 50);
//    tempButton.backgroundColor=UIColorFromRGB(0xe5e5e5);
//    [self.view addSubview:tempButton];
//    //ADD_TARGET_INSIDE(tempButton, tempPress:);
    
    
//    [tempButton addActionHandler:^(NSInteger tag) {
//        NSLog(@"");
//        
//        NSLog(@"ada");
//    }];
    
//    ALLOC(UIView, viwww);
    
    
//    [tempButton addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
//        NSLog(@"ada");
//
//    }];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)loginBtnPress:(id)sender {
    if (self.phoneNumber.text.length == 0) {
        
        [self.view makeToast:@"账号不能为空！"];
        
        return;
        
    }
    if (self.passWord.text.length==0) {
        
        [self.view makeToast:@"密码不能为空！"];
        
        return;
        
    }
    
    [self addProgressHUD];
    
    //2.设置登录参数
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:self.phoneNumber.text, @"mobile", self.passWord.text, @"password", [NSString stringWithFormat:@"%d",type], @"type", nil];
    
    NSLog(@"登录字典：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/artisan/login" parameters:dictionary option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
        
        [self removeStatuslable];
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            //            Singleton *single = [Singleton shareSingleton];
            //            single.artisanId = responseObject[@"artisanId"];
            //            single.name = responseObject[@"name"];
            
            [[TMCache sharedCache]setObject:responseObject[@"artisanId"] forKey:@"artisanId"];
            [[TMCache sharedCache]setObject:responseObject[@"name"]forKey:@"artisanName"];
            [[TMCache sharedCache]setObject:self.phoneNumber.text forKey:@"phoneNumber"
             ];
            /**
             *  缓存登录类型用于再次进入使用
             */
            [[TMCache sharedCache]setObject:[NSString stringWithFormat:@"%d",type] forKey:@"TeacherType"];
            
            
            if (type==4) {
                ALLOC(LicensedTabBarViewController, view);
                [self presentViewController:view animated:YES completion:nil];
            }
            
            if (type==1) {
                
                
                
                ALLOC(MassageTabBarViewController, view);
                
                //                [self pushNextViewController:view];
                
                [self presentViewController:view animated:YES completion:nil];
                
                
            }
            
        }
        else{
            [self showFailed:responseObject[@"message"]];
            
        }
        
        NSLog(@"%@",responseObject);
        
        
    } failur:^(NSError *error) {
        NSLog(@"%@",error);
        [self removeStatuslable];
        [self showFailed:nil];
        
    }];
    
    
    
}




/**
 *  暂时不用
 *
 *  @param btn <#btn description#>
 */

-(void)_tuiNaDocors:(UIButton *)btn{
    [_tuiNaDoctors setTitleColor:UIColorFromRGB(0x00ccc2) forState:UIControlStateNormal];
    [_zhiYeDoctor setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
    _tuiNaDoctors.backgroundColor=[UIColor whiteColor];
    _zhiYeDoctor.backgroundColor=[UIColor clearColor];
    _daoHangImg.hidden=YES;
    _tuiNaDoctorDaoHangImg.hidden=NO;
    NSLog(@"-------%@",self.view.subviews);
    
    //        UIView
    
    //         [UIView animateWithDuration:2 animations:^{
    //             _daoHangImg.frame=CGRectMake(300, _daoHangImg.top, _daoHangImg.width, _daoHangImg.width);
    //
    //
    //         }];
    
    NSLog(@"%f",_daoHangImg.left);
    
    type=1;
    
    
    
}

-(void)zhiyePress:(UIButton *)btn{
    
    
    _tuiNaDoctors.backgroundColor=[UIColor clearColor];
    _zhiYeDoctor.backgroundColor=[UIColor whiteColor];
    [_tuiNaDoctors setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
    
    [_zhiYeDoctor setTitleColor:UIColorFromRGB(0x00ccc2) forState:UIControlStateNormal];
    _daoHangImg.hidden=NO;
    _tuiNaDoctorDaoHangImg.hidden=YES;
    type=4;
    
    NSLog(@"-------%@",self.view.subviews);
    
    
    
    
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
